// pages/search/search.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    searchValue:"",
    searchData:[],
    hotSearchData:["冰箱","洗衣机","小米","空军","漫画","手机","苹果","三只松鼠","杀人刀","杀人书"],
    del:false
  },
  inputchange:function(value){
    this.searchValue = value.detail.value;
  },
  search:function(){
    if(this.searchValue != undefined && this.searchValue != ""){
      this.del = true;

      this.removeDup(this.searchValue);

      this.data.searchData.unshift(this.searchValue);

      this.setData({
        searchValue : "",
        searchData:this.data.searchData,
        del:this.del
      })

      wx.navigateTo({
        url: '../searchList/searchList?searchValue='+this.searchValue
      })
    }
  },
  delhistory(){
    this.searchData = [];
    wx.setStorage({
      data: this.data.searchData,
      key: 'data',
    })
    this.del = false;

    this.setData({
      del:this.del,
      searchData:this.searchData
    })
  },
  historySearch(method){
    let id = method.target.id;
    wx.navigateTo({
      url: '../searchList/searchList?searchValue='+this.data.searchData[id]
    })
  },
  hotSearch(method){
    let id = method.target.id;
    this.del = true;
    this.removeDup(this.data.hotSearchData[id])
    this.data.searchData.unshift(this.data.hotSearchData[id]);
    this.setData({
      searchData:this.data.searchData,
      del:this.del
    })
    wx.navigateTo({
      url: '../searchList/searchList?searchValue='+this.data.hotSearchData[id]
    });
  },
  removeDup(value){
    for(let i=0; i<this.data.searchData.length; i++){
      if(this.data.searchData[i] == value){
        this.data.searchData.splice(i,1);
      }
    }
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    wx.setStorage({
      key: 'data',
      data:[],
    });

    let a = [];
    wx.getStorage({
      key: 'data',
      success:function(res) {
        a = res.data
      }
    });
    this.data.searchData = a;


  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },
  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },
  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },
  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})